package com.OnTimeTeam.OnTime;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class OpeningsCounterIO {
	
	public static int add1ToCounter(String filePath){
		
		int numberOfEnterances = -1;
		
        try {

    		File file = new File(filePath);
        	
    		if(file.createNewFile()) {
    			
	            //Finally - write to the file the node "reminders"
    			FileWriter fStream = new FileWriter(filePath);
    			BufferedWriter out = new BufferedWriter(fStream);
	    		out.write("1");
	    		
	    		numberOfEnterances = 1;
	    		
	    		out.close();

    		}
    		else {
    			
			  FileInputStream fstream = new FileInputStream(filePath);
			  DataInputStream in = new DataInputStream(fstream);
			  BufferedReader br = new BufferedReader(new InputStreamReader(in));

			  numberOfEnterances = Integer.valueOf(br.readLine());

			  PrintWriter writer = new PrintWriter(file);
			  writer.print(Integer.toString(numberOfEnterances+1));
			  writer.close();
			  
			  numberOfEnterances+=1;
			  
			  in.close();
    			
    		}

        }//end of try block
        catch (Exception e) {//catch any exception
            System.out.println(e);
        }
        
        return numberOfEnterances;
        
    }

}
